package com.ms.pda.net

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.*
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.ms.pda.BuildConfig
import com.ms.pda.base.BaseActivity
import com.ms.pda.components.UpdaterDialog
import com.ms.pda.interf.ResponseCallback
import com.ms.pda.model.Constants
import com.ms.pda.model.KeyMap
import com.ms.pda.model.dto.*
import com.ms.pda.repository.DataSource
import com.ms.pda.utils.DialogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import rxhttp.*
import rxhttp.wrapper.annotation.DefaultDomain
import rxhttp.wrapper.exception.ParseException
import rxhttp.wrapper.param.Param
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toFlowResponse
import rxhttp.wrapper.param.toResponse
import java.io.File
import java.util.concurrent.TimeUnit

object Request : DataSource {

    @JvmField
    @DefaultDomain
    var BASE_URL = BuildConfig.BASE_HOST

    private const val UPLOAD = "/storage/produce/upload?deviceNo=设备编号\n"

    private var mLoading: BasePopupView? = null

    fun init() {
        val client: OkHttpClient = OkHttpClient.Builder()
            .callTimeout(Constants.TIME_OUT, TimeUnit.SECONDS)
            .connectTimeout(Constants.TIME_OUT, TimeUnit.SECONDS)
            .readTimeout(Constants.TIME_OUT, TimeUnit.SECONDS)
            .writeTimeout(Constants.TIME_OUT, TimeUnit.SECONDS)
            .build()
        RxHttpPlugins.init(client)
            .setDebug(Constants.DEBUG)
            .setOnParamAssembly { param: Param<*>? ->
                param?.addHeader("Token:", "6f783dc1eec44d72a137964f5a05655e")
                param
            }
    }

    suspend fun upload(
        activity: AppCompatActivity,
        file: File,
        callback: ResponseCallback<Boolean>
    ) {
        RxHttp.postForm(UPLOAD)
            .addQuery("deviceNo", DeviceUtils.getUniqueDeviceId())
//            .add("key", "value")
            .addFile("filename", file)
            .toFlowOkResponse()
            .flowOn(Dispatchers.IO)
            .onStart {
                getLoading(activity).show()
            }
            .catch {
                dismiss()
                callback.onFailed(it)
            }
            .collect {
                dismiss()
                callback.onSuccess(it.code == 200)

            }
    }


    private fun getLoading(context: Context?): Request {
        if (mLoading != null && mLoading!!.isShow) {
            dismiss()
        }
        mLoading = XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .dismissOnTouchOutside(false)
            .dismissOnBackPressed(false)
            .hasShadowBg(false)
            .asLoading()
        return this
    }

    private fun show() {
        if (mLoading == null) return
        if (mLoading!!.isShow) return
        mLoading?.show()
    }

    private fun dismiss() {
        mLoading?.smartDismiss()
    }

}